# coding=utf-8
"""
copyright (c) 2009, paketka@gmail.com et. al
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

	* Redistributions of source code must retain the above copyright notice,
	  this list of conditions and the following disclaimer.

	* Redistributions in binary form must reproduce the above copyright notice,
	  this list of conditions and the following disclaimer in the documentation
	  and/or other materials provided with the distribution.

	* Neither the name of the <ORGANIZATION> nor the names of its contributors
	  may be used to endorse or promote products derived from this software
 	  without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""

import datetime
import StringIO
import re
import os

from django.shortcuts import render_to_response, get_object_or_404, get_list_or_404
from django.template import RequestContext
from django import template
from django.http import HttpResponse
from django.core.paginator import Paginator, EmptyPage, InvalidPage
from django.http import Http404
from django.core import serializers
from django.utils import simplejson
from django.db.models import Q
from django.core.exceptions import ObjectDoesNotExist
from django.utils.translation import ugettext as _
from django.core.cache import cache

from psp.models import *
from GChartWrapper import *

import psp.views

#from views import get_date_data
#from views import get_date_data_group


def filter_mp_list(pageObjects, filterExp, attribs = (
											u'title',
											u'name',
											u'surname',
											u'titleLast',
											u'partyName',
											u'divisionName'
										)
								):
	"""
	filters objects from pageObjects set. filterExp is parameter gathered from
	GET. it can have any of form bellow:
		ing pRaha	(will return all persons with ing title from prague)

		mudr karel	(will return all persons with mudr title and name/surname
					matching to karel)

		libor vlk	(will return all persons who are called libor vlk or vlk
					libor)

		pavel pavel	(will return all persons who are called pavel pavel)

		josef praha	(will return all persons who are living in praha or are
					called josef praha)
		...

	function will split filterExp to words, and then it will apply each word
	to every following attribute:
		person__title
		person__name
		person__surname
		person__titleLast
		mpClub
		divName

	the resulting lists are cobnined so we get the most accurate set, which
	members match to all criteria.
	"""
	def match_attrib(pageObjects, filterExp, attrib):
		pattern = re.compile(u'.*' + filterExp + u'.*', re.U | re.I)
		retVal = []
		
		for p in pageObjects:
			t = None
			if hasattr(p, attrib):
				t = getattr(p, attrib)
				if pattern.match(t) != None:
					retVal.append(p)

		return retVal

	
	retVal = []
	matchList = []
	for f in filterExp.split(' '):
		matchSet = []
		for a in attribs:
			tmpList = match_attrib(pageObjects, f, a)
			if len(tmpList) > 0:
				matchSet += tmpList

		pageObjects = matchSet

	return pageObjects


def list_persons_json(request, yearStart = None, yearEnd = None):
	pageNo = 1
	rows = 25
	filterExp = ''

	try:
		rows = int(request.GET.get('rows'))
		pageNo = int(request.GET.get('page'))
		filterExp = request.GET.get('filter__exp')
		
		if filterExp == None:
			filterExp = ''
	except:
		pass

	term = None
	if yearStart != None and yearEnd != None:
		term = TermOfOffice.get_term_or_404(yearStart, yearEnd)

	sidx = request.GET.get('sidx')
	sord = request.GET.get('sord')
	desc = (sord != None and sord == 'desc')
	pageObjects = None

	if sidx == None or sidx == '' or sidx == 'person__surname':
		pageObjects = Person.objects.getPersons(desc = desc, term = term)
	elif sidx == 'person__name':
		pageObjects = Person.objects.getPersonsOrderByName(
											desc = desc,
											term = term
										)
	elif sidx == 'person__birthDate':
		pageObjects = Person.objects.getPersonsOrderByAge(
											desc = desc,
											term = term
										)
	elif sidx == 'group__name':
		pageObjects = Person.objects.getPersonsOrderByParty(
											desc = desc,
											term = term
										)
	elif sidx == 'division':
		pageObjects = Person.objects.getPersonsOrderByDivision(
											desc = desc,
											term = term
										)
	elif sidx == 'absences':
		pageObjects = Person.objects.getPersonsOrderByAbsences(
											desc = desc,
											term = term
										)
		
	if filterExp != '':
		pageObjects = filter_mp_list(pageObjects, filterExp)

	paginator = Paginator(pageObjects, rows)
	page = paginator.page(pageNo)
	return render_to_response('persons.json', {
									'pageNo' : pageNo,
									'pagesTotal' : paginator.num_pages,
									'records' : rows,
									'page': page,
									'today': datetime.date.today(),
									'term': term,
									},
								mimetype = 'application/json',
								context_instance = RequestContext(request)
							)


def group_members_json(request, groupSlug):
	pageNo = 1
	rows = 25
	filterExp = ''
	group = get_object_or_404(Group, slug = groupSlug)
	try:
		rows = int(request.GET.get('rows'))
		pageNo = int(request.GET.get('page'))
		filterExp = request.GET.get('filter__exp')
		
		if filterExp == None:
			filterExp = ''
	except:
		pass

	sidx = request.GET.get('sidx')
	if sidx == None or sidx == '':
		sidx = 'person__surname'

	sord = request.GET.get('sord')
	desc = sord != None and sord == 'desc'

	pageObjects = []
	if sidx == 'person__birthDate':
		pageObjects = group.getMembersByAge(desc = desc, distinct = False)
	elif sidx == 'group__name':
		# we still need to use order_page_objects() here, since
		# Membership QS uses two sorting keys (group__name and
		# person__surname), there is no way to tell to use group__name
		# only sorting there
		#
		# TODO: verify the statement above is realy true (check django
		# sources)
		pageObjects = group.getMembersByParty(desc = desc, distinct = False)
	elif sidx == 'division':
		pageObjects = group.getMembersByDivision(desc = desc, distinct = False)
	elif sidx == 'absences':
		pageObjects = group.getMembersByAbsences(desc = desc, distinct = False)
	elif sidx == 'days':
		pageObjects = group.getMembersByDays(desc = desc, distinct = False)
	elif sidx == 'person__surname':
		pageObjects = group.getMembersBySurname(desc = desc, distinct = False)
	elif sidx == 'person__name':
		pageObjects = group.getMembersByName(desc = desc, distinct = False)

	if filterExp != '':
		attribs = (
			u'person__title',
			u'person__name',
			u'person__surname',
			u'person__titleLast',
			u'mpClub',
			u'divName',
			u'post',
		)
		pageObjects = filter_mp_list(pageObjects, filterExp, attribs)

	paginator = Paginator(pageObjects, rows)
	page = paginator.page(pageNo)

	return render_to_response(	'members.json', {
								'term' : group.term,
								'pageNo' : pageNo,
								'pagesTotal' : paginator.num_pages,
								'records' : rows,
								'page' : page,
							},
							mimetype = 'application/json',
								context_instance = RequestContext(request)
							)


def list_meetings_json(request, yearStart, yearEnd):
	term = TermOfOffice.get_term_or_404(yearStart, yearEnd)
	pageNo = 1
	rows = 25

	try:
		rows = int(request.GET.get('rows'))
		pageNo = int(request.GET.get('page'))
	except:
		pass

	sidx = request.GET.get('sidx')
	if sidx == None or sidx == '':
		sidx = 'meetingNo'

	sord = request.GET.get('sord')
	if sord != None and sord == 'desc':
		sidx = '-' + sidx

	pageObjects = Meeting.objects.filter(term = term).order_by(sidx)
	paginator = Paginator(pageObjects, rows)
	page = paginator.page(pageNo)

	return render_to_response(	'meetings.json', {
								'term' : term,
								'pageNo' : pageNo,
								'pagesTotal' : paginator.num_pages,
								'records' : rows,
								'page' : page,
							},
							mimetype = 'application/json',
							context_instance = RequestContext(request)
						)


def get_group_poll_json(request, groupId, pollId):
	poll = get_object_or_404(Poll, id = int(pollId))
	group = get_object_or_404(Group, id = int(groupId))
	gs = GroupStats.objects.get(group = group, poll = poll)
	pollChart = gs.getResultChart()
	json = u''

	if pollChart == None:
		json = u"{ pollPie : '%s',\npartyPie : ''}" % ('<img src="http://127.0.0.1:8000/media/images/neexistuje.png"/>')
		return HttpResponse(json, mimetype = 'application/json')

	if group.type != 'KLUB':
		partyPie = group.getPartyPie(poll.date)
		if partyPie == None:
			json = u"{ pollPie : '%s',\npartyPie : '' }" % pollChart.img()
		else:
			json = u"{ pollPie : '%s',\npartyPie : '%s' }" % (pollChart.img(), partyPie.img())
	elif pollChart:
		json = u"{ pollPie : '%s',\npartyPie : '' }" % pollChart.img()
	else:
		json = u"{ pollPie : '',\npartyPie : '' }"

	return HttpResponse(json, mimetype = 'application/json')


def get_polls_in_group(request, groupId, pollId):
	def strcmp(a, b):
		if a.result < b.result:
			return -1
		elif a.result > b.result:
			return 1
		else:
			return 0


	group = get_object_or_404(Group, id = int(groupId))
	poll = get_object_or_404(Poll, id = int(pollId))
	pageNo = 1
	rows = 25
	filterExp = ''
	try:
		rows = int(request.GET.get('rows'))
		pageNo = int(request.GET.get('page'))
		filterExp = request.GET.get('filter__exp')
		
		if filterExp == None:
			filterExp = ''
	except:
		pass

	sidx = request.GET.get('sidx')
	if sidx == None or sidx == '':
		sidx = 'surname'

	knownSidx = [ 
		'surname',
		'name',
		'party',
		'area',
		'result',
	]
	if not sidx in knownSidx:
		sidx = 'surname'

	sord = request.GET.get('sord')
	desc = sord != None and sord == 'desc'
	
	pageObjects = []
	if sidx == 'party':
		pageObjects = group.getMembersByParty(
							desc = desc,
							dateStart = poll.date
						)
	elif sidx == 'area':
		pageObjects = group.getMembersByDivision(
							desc = desc,
							dateStart = poll.date
						)
	elif sidx == 'surname':
		pageObjects = group.getMembersBySurname(
							desc = desc,
							dateStart = poll.date
						)
	elif sidx == 'name':
		pageObjects = group.getMembersByName(
							desc = desc,
							dateStart = poll.date
						)
	else:
		pageObjects = group.getMembersBySurname(
							desc = desc,
							dateStart = poll.date
						)

	if filterExp != '':
		attribs = (
			u'person__title',
			u'person__name',
			u'person__surname',
			u'person__titleLast',
			u'mpClub',
			u'divName',
			u'post',
		)
		pageObjects = filter_mp_list(pageObjects, filterExp, attribs)
		
	for mp in pageObjects:
		mp.result = MpVote.objects.get(person = mp, poll = poll).getResult()

	if sidx == 'result':
		if desc:
			pageObjects.sort(strcmp)
			pageObjects.reverse()
		else:
			pageObjects.sort(strcmp)

	paginator = Paginator(pageObjects, rows)
	page = paginator.page(pageNo)

	return render_to_response(	'poll_groups.json', {
									'pageNo' : pageNo,
									'pagesTotal' : paginator.num_pages,
									'records' : rows,
									'page' : page,
									'term' : poll.meeting.term,
								},
								mimetype = 'application/json',
								context_instance = RequestContext(request)
							)


def filter_procedural(qs, request):
	"""
	If request is request object. If request containse filterProcedural=1
	parameter, then function applies  procedural filter to query set 'qs'.
	Otherwise it returns queryset untouched.

	expected queryset must be Poll class queryset
	returns queryset
	"""
	filterProcedural = False
	try:
		tmp = request.GET.get('filterProcedural')
		filterProcedural = (tmp != None and tmp == '1')
	except:
		pass

	retVal = qs
	if filterProcedural:
		retVal = qs.filter(~Q(name__iregex = '.*Procedurální hlasování'))

	return retVal


def get_polls_in_day(request, year, month, day):
	pageNo = 1
	rows = 25
	filterExp = ''
	try:
		rows = int(request.GET.get('rows'))
		pageNo = int(request.GET.get('page'))

		filterExp = request.GET.get('filter__exp')
		if filterExp == None:
			filterExp = ''
	except:
		pass

	date = datetime.datetime(
					year = int(year),
					month = int(month),
					day = int(day)
				)

	polls = Poll.objects.filter(date = date)
	polls = filter_procedural(polls, request)

	if filterExp != '':
		regex = u''
		for i in filterExp.split(' '):
			regex = '.*' + i + '.*'
		polls = polls.filter(
							date = date,
							name__iregex = regex
						).order_by('pollId')

	paginator = Paginator(polls, rows)
	page = paginator.page(pageNo)

	return render_to_response(	'polls_in_day.json', {
									'pageNo' : pageNo,
									'pagesTotal' : paginator.num_pages,
									'records' : rows,
									'page' : page,
								},
								mimetype = 'application/json',
								context_instance = RequestContext(request)
							)


def get_meetings_in_month(request, year, month):
	polls = Poll.objects.filter(
				date__month = int(month),
				date__year = int(year)
			).distinct('date').order_by('date')

	dateMap = [0 for i in range(32)]
	for p in polls:
		dateMap[p.date.day] = 1

	buf = StringIO.StringIO()
	buf.write(dateMap)
	json = buf.getvalue()
	buf.close()

	return HttpResponse(json, mimetype = 'application/json')


def get_votes_common(request, mp, date, votes):
	pageNo = 1
	rows = 25
	try:
		rows = int(request.GET.get('rows'))
		pageNo = int(request.GET.get('page'))
	except:
		pass

	paginator = Paginator(votes, rows)
	page = paginator.page(pageNo)
	
	pageData = []
	for i in page.object_list:
		record = {}
		record['vote'] = i
		groupResult = GroupStats.objects.get(
										poll = i.poll,
										group = mp.getPartyClub(date)
									).result
	
		if i.result == groupResult:
			record['loyal'] = _('S klubem')
		else:
			record['loyal'] = _('Proti klubu')
		
		pageData.append(record)

	return render_to_response(	'mp_votes.json', {
									'pageNo' : pageNo,
									'pagesTotal' : paginator.num_pages,
									'records' : rows,
									'page' : pageData,
								},
								mimetype = 'application/json',
								context_instance = RequestContext(request)
							)


def filter_procedural_mpvote(qs, request):
	"""
	If request is request object. If request containse filterProcedural=1
	parameter, then function applies  procedural filter to query set 'qs'.
	Otherwise it returns queryset untouched.

	expected queryset must be MpVote class queryset
	returns queryset
	"""
	filterProcedural = False
	try:
		tmp = request.GET.get('filterProcedural')
		filterProcedural = (tmp != None and tmp == '1')
	except:
		pass

	retVal = qs
	if filterProcedural:
		retVal = qs.filter(~Q(poll__name__iregex = '.*Procedurální hlasování'))
		
	return retVal


def get_aye(request, mpSlug, year, month, day):
	date = datetime.date(
				day = int(day),
				month = int(month),
				year = int(year)
			)
	mp = get_object_or_404(Person, slug = mpSlug)
	votes = MpVote.objects.filter( 
								person = mp,
								result = 'A',
								poll__date = date
						)

	votes = filter_procedural_mpvote(votes, request)
	return get_votes_common(request, mp, date, votes)


def get_nay(request, mpSlug, year, month, day):
	date = datetime.date(
				day = int(day),
				month = int(month),
				year = int(year)
			)
	mp = get_object_or_404(Person, slug = mpSlug)
	votes = MpVote.objects.filter( 
								person = mp,
								result = 'N',
								poll__date = date
						)

	votes = filter_procedural_mpvote(votes, request)
	return get_votes_common(request, mp, date, votes)


def get_refrains(request, mpSlug, year, month, day):
	date = datetime.date(
				day = int(day),
				month = int(month),
				year = int(year)
			)
	mp = get_object_or_404(Person, slug = mpSlug)
	votes = MpVote.objects.filter(
								person = mp,
								result = 'Z',
								poll__date = date
						)

	votes = filter_procedural_mpvote(votes, request)
	return get_votes_common(request, mp, date, votes)


def get_absences(request, mpSlug, year, month, day):
	date = datetime.date(
				day = int(day),
				month = int(month),
				year = int(year)
			)
	mp = get_object_or_404(Person, slug = mpSlug)

	votes = MpVote.objects.filter(
				Q(person = mp),
				Q(poll__date = date),
				~Q(result = 'A'),
				~Q(result = 'N'),
				~Q(result = 'Z')
			)

	votes = filter_procedural_mpvote(votes, request)
	return get_votes_common(request, mp, date, votes)


def get_date_data_json(request, mpSlug, termId, year, month, day):
	"""
	Returns dynamic data for mp_term_overview for particular day.
	"""
	date = datetime.date(
				day = int(day),
				month = int(month),
				year = int(year)
			)
	mp = get_object_or_404(Person, slug = mpSlug)
	term = get_object_or_404(TermOfOffice, id = int(termId))

	return render_to_response('date_data.json', 
								psp.views.get_date_data(mp, date, term),
								mimetype = 'application/json',
								context_instance = RequestContext(request)
							)


def get_loyal_pie_term(request, mpSlug, groupId, termId):
	"""
	Returns loyal pie chart for mp, group in given term
	"""
	mp = get_object_or_404(Person, slug = mpSlug)
	group = get_object_or_404(Group, id = int(groupId))
	term = get_object_or_404(TermOfOffice, id = int(termId))
	polls = Poll.objects.filter(meeting__term = term).order_by('date')
	dateStart = polls[0].date
	dateEnd = polls[len(polls) - 1].date

	if group.type == 'KLUB':
		json = "'<img src=\"%s\"/>'" % mp.getLoyalChartForDates(
											group,
											dateStart,
											dateEnd
										)
	else:
		partyPie = group.getPartyPie()
		partyPie.size(330, 80)
		json = "'<img src=\"%s\"/><img src=\"%s\"/>'" % (
					mp.getLoyalChartForDates(group, dateStart, dateEnd),
					partyPie
				)
	
	return HttpResponse(json, mimetype = 'application/json')


def get_loyal_pie_day(request, mpSlug, groupId, year, month, day):
	date = datetime.date(
				year = int(year),
				month = int(month),
				day = int(day)
			)
	mp = get_object_or_404(Person, slug = mpSlug)
	group = get_object_or_404(Group, id = int(groupId))
	
	if group.type == 'KLUB':
		json = "'<img src=\"%s\"/>'" % (
										mp.getLoyalChartForDates(
												dateStart = date,
												group = group),
									)
	else:
		partyPie = group.getPartyPie(dateStart = date)
		partyPie.size(330, 80)
		json = "'<img src=\"%s\"/><img src=\"%s\"/>'" % (
					mp.getLoyalChartForDates(dateStart = date, group = group),
					partyPie
				)

	return HttpResponse(json, mimetype = 'application/json')


def get_group_common(request, groupStats):
	pageNo = 1
	rows = 25
	try:
		rows = int(request.GET.get('rows'))
		pageNo = int(request.GET.get('page'))
	except:
		pass

	paginator = Paginator(groupStats, rows)
	page = paginator.page(pageNo)
	
	return render_to_response(	'group_votes.json', {
									'pageNo' : pageNo,
									'pagesTotal' : paginator.num_pages,
									'records' : rows,
									'page' : page,
								},
								mimetype = 'application/json',
								context_instance = RequestContext(request)
							)

def get_group_aye(request, groupSlug, year, month, day):
	group = get_object_or_404(Group, slug = groupSlug)
	date = datetime.date(
					year = int(year),
					month = int(month),
					day = int(day)
				)
	groupStats = []
	polls = Poll.objects.filter(date = date)
	polls = filter_procedural(polls, request)
	for p in polls:
		gs = None
		try:
			gs = get_object_or_404(GroupStats, group = group, poll = p)
		except ObjectDoesNotExist:
			continue

		if gs.getResult() != 'Pro':
			continue
	
		groupStats.append(gs)

	return get_group_common(request, groupStats)


def get_group_nay(request, groupSlug, year, month, day):
	group = get_object_or_404(Group, slug = groupSlug)
	date = datetime.date(
					year = int(year),
					month = int(month),
					day = int(day)
				)
	groupStats = []
	polls = Poll.objects.filter(date = date)
	polls = filter_procedural(polls, request)
	for p in polls:
		gs = None
		try:
			gs = get_object_or_404(GroupStats, group = group, poll = p)
		except ObjectDoesNotExist:
			continue

		if gs.getResult() != 'Proti':
			continue

		groupStats.append(gs)

	return get_group_common(request, groupStats)


def get_group_refrains(request, groupSlug, year, month, day):
	group = get_object_or_404(Group, slug = groupSlug)
	date = datetime.date(
					year = int(year),
					month = int(month),
					day = int(day)
				)
	groupStats = []
	polls = Poll.objects.filter(date = date)
	polls = filter_procedural(polls, request)
	for p in polls:
		gs = None
		try:
			gs = get_object_or_404(GroupStats, group = group, poll = p)
		except ObjectDoesNotExist:
			continue

		if gs.getResult() != u'Zrželi':
			continue

		groupStats.append(gs)

	return get_group_common(request, groupStats)


def get_group_absences(request, groupSlug, year, month, day):
	group = get_object_or_404(Group, slug = groupSlug)
	date = datetime.date(
					year = int(year),
					month = int(month),
					day = int(day)
				)
	groupStats = []
	polls = Poll.objects.filter(date = date)
	polls = filter_procedural(polls, request)
	for p in polls:
		gs = None
		try:
			gs = get_object_or_404(GroupStats, group = group, poll = p)
		except ObjectDoesNotExist:
			continue

		if gs.getResult() != u'Chyběli':
			continue

		groupStats.append(gs)

	return get_group_common(request, groupStats)


def get_date_data_group_json(request, groupSlug, year, month, day):
	"""
	Returns dynamic data for show_group for particular day.
	"""
	date = datetime.date(
				day = int(day),
				month = int(month),
				year = int(year)
			)
	group = get_object_or_404(Group, slug = groupSlug)

	return render_to_response('date_data_group.json', 
								psp.views.get_date_data_group(group, date),
								mimetype = 'application/json',
								context_instance = RequestContext(request)
							)


def get_accordance_pie_chart_common(request, groupId, otherGroupId, pollQS):
	"""
	returns poll result accordance for group and other group. Groups are
	specified by their ids. The poll result accordance is computed for
	given pill set (pollQS argument).
	"""
	group = get_object_or_404(Group, id = int(groupId))
	otherGroup = get_object_or_404(Group, id = int(otherGroupId))
	loyalChart = group.getAccordanceWithGroup(	otherGroup,
												pollQS[0].date,
												pollQS[len(pollQS) - 1].date
											)

	if loyalChart == None:
		raise Http404

	if otherGroup.type == 'KLUB':
		json = u"{ loyalChart : '%s',\n partyPie: '' }" % loyalChart.img()
	else:
		partyPie = otherGroup.getPartyPie()
		partyPie.size(330, 80)
		json = u"{ loyalChart : '%s',\n partyPie: '%s' }"  % (
										loyalChart.img(),
										partyPie.img()
									)
	
	return HttpResponse(json, mimetype = 'application/json')	


def get_term_accordance_pie_chart_for_group(request, groupId, otherGroupId, termId):
	"""
	Returns json data with pie chart poll accordance for group with other group in
	particular term.
		groupSlug - group we are examining
		otherGroupSlug - group we want to view accordance with
		termId - id of term
	"""
	term = get_object_or_404(TermOfOffice, id = int(termId))
	polls = Poll.objects.filter(meeting__term = term)

	return get_accordance_pie_chart_common(request, groupId, otherGroupId, polls)


def get_day_accordance_pie_chart_for_group(
								request,
								groupId,
								otherGroupId, 
								year,
								month,
								day
							):
	"""
	Same as get_term_accordance_pie_chart_for_group(), but this time for any given day.
	"""
	date = datetime.date(
					day = int(day),
					month = int(month),
					year = int(year)
				)
	polls = Poll.objects.filter(date = date)

	return get_accordance_pie_chart_common(request, groupId, otherGroupId, polls)
